﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using SqlCon;

namespace yfxt
{
    public partial class zy_fyjs : Form
    {
        SqlConnection con;
        SqlDataAdapter da;
        DataTable dt = new DataTable();
        SqlCommandBuilder scbd;

        string constr = "data source=localhost;database=yf;uid=sa;pwd=123;timeout=60";
        public zy_fyjs()
        {
            InitializeComponent();
        }

        private void zy_fyjs_Load(object sender, EventArgs e)
        {

        }
        #region 刷新 获取被锁定医嘱
        private void button1_Click(object sender, EventArgs e)
        {
            dt.Clear();
            string sql_sdyz;
            sql_sdyz = "select c.brch,c.brxm,a.tjsj,"+
                "(case when(a.fyfs=1) then '摆药' when(a.fyfs=2) then '发药' when (a.fyfs=3) then '处方用药' when (a.fyfs=4) then '大输液' when (a.fyfs=5) then '冷藏药品' when (a.fyfs=6) then '其他' when (a.fyfs=7) then '外用及其他' end) as fyfs,"+
                "(case when (tjbq=19) then '外一区' when(tjbq=20) then '外2区'when (tjbq=21) then '产科' when (tjbq=22) then '妇科' when (tjbq=23) then '内科' when (tjbq=24) then '儿科' when (tjbq=25) then '新生儿病区' end) as tjbq," +
                "a.tjgh,a.tjxh," +
                "(case when(a.fybz=0) then '未发' when(a.fybz=1) then '已发' when (a.fybz=2) then '退回' when(a.fybz=3) then '锁定' end) as fybz " +
                "from bq_tj01 a," +
                "bq_tj02 b," + 
                "zy_brry c " +
                "where a.fybz=3 "+
                "and a.tjxh=b.tjxh "+
                "and b.zyh=c.zyh "+
                "and CONVERT(varchar(25), a.tjsj, 121) like '"+dateTimePicker1.Value.ToShortDateString()+"%' ";
            con = new SqlConnection(constr);
            con.Open();
            da = new SqlDataAdapter(sql_sdyz,con);
            scbd = new SqlCommandBuilder(da);
            da.Fill(dt);
            dataGridView2.Columns[0].DataPropertyName = dt.Columns[0].ToString();
            dataGridView2.Columns[1].DataPropertyName = dt.Columns[1].ToString();
            dataGridView2.Columns[2].DataPropertyName = dt.Columns[2].ToString();
            dataGridView2.Columns[3].DataPropertyName = dt.Columns[3].ToString();
            dataGridView2.Columns[4].DataPropertyName = dt.Columns[4].ToString();
            dataGridView2.Columns[5].DataPropertyName = dt.Columns[5].ToString();
            dataGridView2.Columns[6].DataPropertyName = dt.Columns[6].ToString();
            dataGridView2.Columns[7].DataPropertyName = dt.Columns[7].ToString();
            dataGridView2.DataSource = dt;
        }
        #endregion

        #region 退出
        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region 解锁
        private void button2_Click(object sender, EventArgs e)
        {
            //int res;
            int rowcount;
            int tjxh;
            string sql_tj01, sql_tj02;
            for (rowcount = 0; rowcount < dataGridView2.RowCount;rowcount++ )
            {
                tjxh = int.Parse(dt.Rows[rowcount]["tjxh"].ToString());
                SqlParameter[] para = new SqlParameter[] 
                {
                    new SqlParameter("@tjxh",tjxh)
                };
                sql_tj01 = "update bq_tj01 set fybz=0 where tjxh=@tjxh ";
                sql_tj02 = "update bq_tj01 set fybz=0 where tjxh=@tjxh ";
                try
                {
                    SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction,CommandType.Text,sql_tj02,para);
                    SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction,CommandType.Text,sql_tj01,para);
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            button1_Click(sender, e);
            MessageBox.Show("解锁成功");
        }
        #endregion
    }
}
